pacman::p_load( "tidyverse", "data.table", "estimatr","xtable")
#functions
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

#load & select B5 data-------------
load("Data/Experiment_merged.Rdata")
merged$pid01<-zero1(merged$pid)
merged$libcon01<-zero1(merged$libcon)
merged$approval01<-zero1(6-merged$approval)
merged_complete <- (dplyr::select(merged,race,gender,age,education,passed,id,general,econ,cult,libcon01, pid01, approval01, politics2nd,symbolic,opennessr,conscientiousness))
merged_l <- data.table::melt(data.table(merged_complete),measure.vars=list(x=c("opennessr","conscientiousness")),id.vars=c("id","politics2nd","passed","gender","age","race","education","symbolic","general","cult","econ", "libcon01", "pid01", "approval01"))


b5 <- (dplyr::select(merged_l,race,gender,age,education,id, libcon01, pid01, symbolic, general, approval01, cult, econ, politics2nd, variable, value))
b5$study<-"B5"
names(b5)[names(b5) == "libcon01"] <- "libcon"
names(b5)[names(b5) == "approval01"] <- "approval"
names(b5)[names(b5) == "pid01"] <- "pid"
names(b5)[names(b5) == "value"] <- "closedpersonality"
names(b5)[names(b5) == "variable"] <- "index"
b5$b5participate<-0

#load & select Auth data-------------------
load("Data/Auth_experiment_data.Rdata")
both$b5participate[both$id==1]=0
both$b5participate[both$id==2]=1

auth<- (dplyr::select(both,race,gender,age,education,b5participate, ideology, pid7, symbolic, politicscondition, authoritarianism))
auth$study<-"Auth"
names(auth)[names(auth) == "authoritarianism"] <- "closedpersonality"
names(auth)[names(auth) == "ideology"] <- "libcon"
names(auth)[names(auth) == "politicscondition"] <- "politics2nd"
names(auth)[names(auth) == "pid7"] <- "pid"
auth <- auth %>% mutate(auth, id = row_number())
auth$id<-auth$id+2000
auth$education <- as.factor(auth$education)
auth$pid<-zero1(auth$pid)
auth$libcon<-zero1(auth$libcon)
levels(auth$education)[2]<- "HS or Less"


auth$closedpersonality<-zero1(auth$closedpersonality)
auth$symbolic<-zero1(auth$symbolic)
auth$econ<-NA
auth$cult<-NA
auth$general<-NA
auth$approval<-NA
auth$index<-"authoritarianism"

#merge & create combined data--------------
combined<- rbind(b5, auth)
combined$study<-as.factor(combined$study)
table(combined$study, combined$b5participate)
combined_long <- as.data.frame(data.table::melt(data.table(combined),measure.vars=list(x=c("libcon", "pid", "symbolic", "general", "approval", "cult", "econ")),id.vars=c("id","politics2nd","gender","age","race","education", "study", "b5participate", "index", "closedpersonality")))
#Individual data meta analysis reported in the main text and SI 8---------
pooled_model <- lm_robust(closedpersonality~value*politics2nd+gender+age+race+education +variable + study + b5participate, combined_long,clusters = interaction(combined_long$id,combined_long$study),se_type = "stata")
studycomm <- paste0("\\hline \n \\multicolumn{5}{l}",
               "{\\scriptsize{Standard errors are clustered at the study-individual respondent. One-sided p-values are reported}} \n")

pooled_model %>% tidy  %>%  select(-term, -conf.low, -conf.high, -df, -outcome) %>%  mutate(p.value = p.value / 2)  %>% add_column(Variable = c("Intercept", "Ideology", "Politics", "Gender: Male", "Gender: Other", "Age", "Race: Hispanic", "Race: Other", "Race: White", "Education: high school", "Education:  some college", "Dimension: PID", "Dimension: Symbolic", "Dimension: General", "Dimension: Trump approval", "Dimension: Cultural", "Dimension: Economic", "Study: Big Five", "Participated in both studies", "Ideology * Politics"), .before = "estimate") %>% setNames(., c("Variable", "b", "SE", "t-statistic", "p-value (one-sided)")) %>% xtable(caption="Results belonging to the Meta-Analysis across the Big Five and Authoritarianism", digits=c(0,0, 2,2,2,2)) %>% print( file="meta_table.tex", label = "tab:meta", caption.placement = "top", include.rownames=FALSE)

#SI 9.1 conditional effect upon Age-----------------
pooled_age <- lm_robust(closedpersonality~value*politics2nd*age+gender+race+education +variable + study + b5participate, combined_long,clusters = interaction(combined_long$id,combined_long$study),se_type = "stata")
summary(pooled_age)
pooled_age %>% tidy  %>%  select(-term, -conf.low, -conf.high, -df, -outcome) %>%  mutate(p.value = p.value)

#SI 9.2 conditional effect upon education----------------
combined_long$edu01<-ifelse(combined_long$education=="College+", 1,0)
pooled_edu <- lm_robust(closedpersonality~value*politics2nd*edu01+age+gender+race +variable + study, combined_long,clusters = interaction(combined_long$id,combined_long$study),se_type = "stata")
